package com.lkj.youmeng.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.lkj.youmeng.R;
import com.lkj.youmeng.data.FriendData;

import java.util.ArrayList;

public class FriendAdapter extends RecyclerView.Adapter<FriendAdapter.Holder> {
    private Context mContext;
    private ArrayList<FriendData> list = new ArrayList<>();

    public FriendAdapter(Context mContext, ArrayList<FriendData> list) {
        this.mContext = mContext;
        this.list = list;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.friend_item, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int position) {

        Glide.with(mContext).asBitmap().load(list.get(position).getPic()).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                holder.publishPic.setImageBitmap(resource);
            }
        });
        holder.name.setText(list.get(position).getName());
        holder.msg.setText(list.get(position).getMsg());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class Holder extends RecyclerView.ViewHolder{
        private ImageView publishPic;
        private TextView name,msg;
        public Holder(@NonNull View itemView) {
            super(itemView);
            publishPic = itemView.findViewById(R.id.publish_pic);
            name = itemView.findViewById(R.id.item_name);
            msg = itemView.findViewById(R.id.item_msg);
        }
    }
}
